package com.etc.controller;

import com.etc.entity.Video;
import com.etc.service.UserService;
import com.etc.service.VideoService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2021-10-18 21:07:18
 */
@Controller
@RequestMapping("video")
public class VideoController {
    /**
     * 服务对象
     */
    @Autowired
    VideoService service;

    /**
     * 查询所以视频
     * @return
     */
    @RequestMapping("getQueryVideo")
    @ResponseBody
    public PageInfo<Video> getQueryVideo(@RequestParam(value = "pageNum") int pageNum,
                                         @RequestParam(value = "pageSize") int pageSize,
                                         @RequestParam(value = "typeName") String typeName){
        PageInfo<Video> pageInfo = service.queryVideo(pageNum,pageSize,typeName);
        return pageInfo;
    }
    /**
     * 跳转视频
     * @return
     */
    @RequestMapping("skipVideo")
    public String skipVideo(){
        return "video/video";
    }
    /**
     * 跳转视频播放
     * @return
     */
    @RequestMapping("playVideo")
    public ModelAndView playVideo(@RequestParam(value = "videoPath") String videoPath
            ,@RequestParam(value = "videoName") String videoName
            ,@RequestParam(value = "videoId") String videoId){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("videoPath",videoPath);
        modelAndView.addObject("videoName",videoName);
        modelAndView.addObject("videoId",videoId);
        modelAndView.setViewName("video/videoPlay");
        return modelAndView;
    }
    /**
     * 视频播放
     * @return
     */
    @RequestMapping("playVideos")
    public void playVideos(@RequestParam(value = "videoPath") String videoPath, HttpServletRequest request, HttpServletResponse response) throws IOException {
            response.setCharacterEncoding("utf-8");
            //返回的数据类型
            response.setContentType("application/pdf");
            //响应头
            response.setHeader("Content-Disposition", "attachment;fileName=" + videoPath);
            InputStream inputStream=null;
            OutputStream outputStream=null;
            //路径
            String path ="E:\\video"+ File.separator+File.separator;
            byte[] bytes = new byte[2048];
            try {
                File file=new File(path,videoPath);
                inputStream = new FileInputStream(file);
                outputStream = response.getOutputStream();
                int length;
                //inputStream.read(bytes)从file中读取数据,-1是读取完的标志
                while ((length = inputStream.read(bytes)) > 0) {
                    //写数据
                    outputStream.write(bytes, 0, length);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                //关闭输入输出流
                if(outputStream!=null) {
                    outputStream.close();
                }
                if(inputStream!=null) {
                    inputStream.close();
                }
            }
        }
    /**
     * 跳转视频管理
     * @return
     */
    @RequestMapping("skipVideoManage")
    public String skipVideoManage(){
        return "backstage/video/video_all";
    }
    /**
     * 按id删除视频
     * @return
     */
    @RequestMapping("deleteVideo")
    @ResponseBody
    public boolean deleteVideo(@RequestParam(value = "videoId") String videoId){
        return service.delete(videoId);
    }
    /**
     * 搜索视频
     * @return
     */
    @RequestMapping("getSeekVideo")
    @ResponseBody
    public PageInfo<Video> getSeekVideo(@RequestParam(value = "pageNum") int pageNum,
                                         @RequestParam(value = "pageSize") int pageSize,
                                         @RequestParam(value = "videoName") String videoName){
        PageInfo<Video> pageInfo = service.seekVideo(pageNum,pageSize,videoName);
        return pageInfo;
    }

}

